package com.zou.mall.product.service.impl;

import com.zou.mall.product.service.CategoryBrandRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zou.common.utils.PageUtils;
import com.zou.common.utils.Query;

import com.zou.mall.product.dao.CategoryDao;
import com.zou.mall.product.entity.CategoryEntity;
import com.zou.mall.product.service.CategoryService;

import javax.annotation.Resource;


@Slf4j
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Resource
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {

        //只查询show_status为1的数据
        QueryWrapper<CategoryEntity> queryWrapper =
                new QueryWrapper<CategoryEntity>().eq("show_status",1);

        List<CategoryEntity> entities = this.baseMapper.selectList(queryWrapper);
        List<CategoryEntity> result = entities.stream().filter(item -> 0 == item.getParentCid())
                .map(menu -> {
                    menu.setChildren(getChildren(menu, entities));
                    return menu;
                }).sorted((a, b) -> {
                    return (a == null ? 0 : a.getSort()) - (b == null ? 0 : b.getSort());
                }).collect(Collectors.toList());
        return result;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO 1.检查当前删除的菜单，是否被别的地方引用
        //逻辑删除优于物理删除
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public int removeBatchIdsLogic(List<Long> list) {
        //逻辑删除在此
        return categoryDao.removeBatchIdsLogic(list);
    }

    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
    }

    @Override
    public List<CategoryEntity> getLevel1Catagories() {
        log.info("查询一级菜单 start");
        long start = System.currentTimeMillis();
        List<CategoryEntity> parent_cids = this.list(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
        log.info("查询一级菜单时间:"+(System.currentTimeMillis()-start));
        return parent_cids;
    }

    /**
     * 获取子类商品类型集合
     *
     * @param p        父类
     * @param entities 子类
     * @return
     */
    private List<CategoryEntity> getChildren(CategoryEntity p, List<CategoryEntity> entities) {
        List<CategoryEntity> collect = entities.stream()
                .filter(a -> a.getParentCid() == p.getCatId())
                .map(a -> {
                    a.setChildren(getChildren(a, entities));
                    return a;
                }).sorted((a, b) -> {
                    return (a == null ? 0 : a.getSort()) - (b == null ? 0 : b.getSort());
                })
                .collect(Collectors.toList());
        return collect;
    }

}